package com.thb.business.dao.Impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.thb.business.dao.IProductDao;
import com.thb.business.model.Product;

@Repository
public class IProductDaoImpl implements IProductDao {

	@Autowired
	private JdbcTemplate jTemplate;

	@Override
	public List<Product> findByPara(Product para) {
		StringBuilder sql = new StringBuilder("");
		sql.append("select a.* from t_product a limit ?,?");
		Object[] arg = new Object[]{(para.getPage()-1)*para.getPageSize(),para.getPageSize()};
		
		return jTemplate.query(sql.toString(), arg,new RowMapper<Product>() {
			
			public Product mapRow(ResultSet rs, int no) throws SQLException {

				return jsonMapper(rs, no);
			}
		});
	}

	private Product jsonMapper(ResultSet rs, int no) throws SQLException {

		Product vo = new Product();
		vo.setId(rs.getInt("id"));
		vo.setImg(rs.getString("img"));
		vo.setProName(rs.getString("pro_name"));
		vo.setPrice(rs.getInt("price"));
		
		return vo;
	}

	@Override
	public int countByPara(Product para) {
		StringBuilder sql = new StringBuilder("");
		sql.append("select a.* from t_product a");
		Object[] arg = new Object[]{};
		return jTemplate.queryForInt(sql.toString(), arg);
	}
}
